package com.open.capacity.recuritment.service.impl;

import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.SysUser;
import com.open.capacity.common.util.PageUtil;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.recuritment.Dto.RecruitmentDto;
import com.open.capacity.recuritment.dao.IRecommendDao;
import com.open.capacity.recuritment.service.IRecommendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * @description:
 * @date:2021/5/25 下午5:34
 * @author:jxy
 */
@Service
public class RecommendServiceImpl implements IRecommendService {
    @Resource
    private IRecommendDao iRecommendDao;

    @Override
    public PageResult<RecruitmentDto> findJobOffers(Map<String, Object> params) {
        try {
            List<RecruitmentDto> list = Collections.emptyList();
            int total = iRecommendDao.count(params);
            if (total > 0) {
                PageUtil.pageParamConver(params, true);
                list = iRecommendDao.findJobOffers(params);
            }
            return PageResult.<RecruitmentDto>builder().data(list).code(0).count((long) total).build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public PageResult<SysUser> findAllUnemployed() {
        try {
            List<SysUser> allUnemployed = iRecommendDao.findAllUnemployed();
            return PageResult.<SysUser>builder().data(allUnemployed).code(0).build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public void pushUnemployed(Map<String, Object> map) {
        try {
            String userId = (String) map.get("userId");
            String recruitmentId = null;
            String companyId = null;
            String sourceStr = (String) map.get("reId");
// 最多分割字符
            int maxSplit = 19;
            String[] sourceStrArray = sourceStr.split(":", maxSplit);
            for (int i = 0; i < sourceStrArray.length; i++) {
                recruitmentId = sourceStrArray[0];
                companyId = sourceStrArray[1];
            }
            iRecommendDao.pushUnemployed(userId, companyId, recruitmentId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

}
